<template>
  <nav class="nav-bar">
    <div class="nav-system-name">
      <img src="/png/title.png" alt="图标" class="nav-icon">
      简易饮食营养管控助手
    </div>
    <div class="nav-right-items">
      <router-link to="/" class="nav-item">首页</router-link>
      <router-link to="/my" class="nav-item">用户</router-link>
      <router-link to="/about" class="nav-item">关于我们</router-link>
      
      <!-- 登录状态显示 -->
      <template v-if="!isLoggedIn">
        <div class="nav-item login-text">请先登录</div>
      </template>
      <template v-else>
        <div class="nav-item user-welcome">
          <span class="welcome-text">欢迎，{{ userName }}</span>
          <button @click="logout" class="logout-btn">登出</button>
        </div>
      </template>
    </div>
  </nav>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/counter' // 引入userStore

const router = useRouter()
const isLoggedIn = ref(false)
const userName = ref('')

// 统一状态检查方法
const updateLoginState = () => {
  isLoggedIn.value = localStorage.getItem('isLoggedIn') === 'true'
  userName.value = localStorage.getItem('userName') || ''
}

// 初始化检查
onMounted(() => {
  updateLoginState()
  // 添加全局事件监听
  window.addEventListener('loginStateChanged', updateLoginState)
  window.addEventListener('storage', updateLoginState)
})

// 清理监听
onUnmounted(() => {
  window.removeEventListener('loginStateChanged', updateLoginState)
  window.removeEventListener('storage', updateLoginState)
})

// 登出功能
const logout = () => {
  localStorage.removeItem('isLoggedIn')
  localStorage.removeItem('userName')
  const userStore = useUserStore()
  userStore.clearUserSession() // 调用清空user_id的方法
  // 触发全局事件通知其他组件
  window.dispatchEvent(new Event('loginStateChanged'))
  router.push('/')
}
</script>

<style scoped>
/* 保持原有导航栏样式完全不变 */
.nav-bar {
  width: 100%;
  background-color: #1a73e8;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 30px;
  margin-bottom: 20px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
  transition: background-color 0.3s ease, transform 0.3s ease;
  position: relative;
  z-index: 100;
}

.nav-bar:hover {
  background-color: #155da0;
  transform: translateY(-2px);
}

.nav-right-items {
  display: flex;
}

.nav-item {
  color: white;
  cursor: pointer;
  font-size: 18px;
  margin-left: 30px;
  text-decoration: none;
  transition: color 0.3s ease, transform 0.3s ease;
  position: relative;
}

.nav-item:hover {
  color: #e0f2fe;
  transform: translateY(-2px);
}

.nav-item::before {
  content: "";
  position: absolute;
  left: 0;
  bottom: -5px;
  width: 0;
  height: 2px;
  background-color: #e0f2fe;
  transition: width 0.3s ease;
}

.nav-item:hover::before {
  width: 100%;
}

.nav-system-name {
  color: white;
  font-size: 22px;
  font-weight: bold;
  cursor: default;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: center;
}

.nav-icon {
  width: 30px;
  height: 30px;
  margin-right: 10px;
}
</style>